package com.steve.xfood.controller;

import com.steve.xfood.dto.OrderDTO;
import com.steve.xfood.enums.ResultEnum;
import com.steve.xfood.exception.XFoodException;
import com.steve.xfood.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 卖家端后台
 * @author steve
 * @date 2020/5/20 13:48
 */
@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用于买家端订单详情展示
     * @param page 第几页 从第一页开始
     * @param size 每页显示的数据条数
     * @param map 用于封装数据，静态页面显示
     * @return ModelAndView 返回静态页面
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "size",defaultValue = "5") int size,
                             Map<String, Object> map) {
        //PageRequest pageRequest = new PageRequest(page, size); springboot 1.5
        PageRequest pageRequest = PageRequest.of(page-1, size);
        Page<OrderDTO> orderDTOPage = orderService.findAll(pageRequest);
        //封装订单数据
        map.put("orderDTOPage", orderDTOPage);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("order/list",map);
    }

    /**
     * 取消订单
     * @param orderId 订单id
     * @param map
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,Map<String, Object> map) {
        try {
            OrderDTO orderDTO = orderService.findOrderById(orderId);
            orderService.cancel(orderDTO);
        } catch (XFoodException e) {
            log.error("【卖家端取消订单】发生异常{}", e);
            //发生错误需要传递错误信息和返回的url
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

    /**
     * 订单详情
     * @param orderId 订单id
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,Map<String, Object> map) {
        OrderDTO orderDTO = null;
        try {
            orderDTO = orderService.findOrderById(orderId);
        } catch (XFoodException e) {
            log.error("【卖家端查询订单详情】 发生异常{}", e);
            //发生错误需要传递错误信息和返回的url
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        //成功则进入订单详情页面
        map.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail", map);
    }

    /**
     * 完结订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId,Map<String, Object> map) {
        try {
            OrderDTO orderDTO = orderService.findOrderById(orderId);
            orderService.finished(orderDTO);
        } catch (XFoodException e) {
            log.error("【卖家端完结订单】 发生异常{}", e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/detail?orderId=" + orderId);
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS.getMessage());
        map.put("url","/sell/seller/order/detail?orderId=" + orderId);
        return new ModelAndView("common/success",map);
    }
}
